/*
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.alee.utils.swing;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

/**
 * User: mgarin Date: 24.08.12 Time: 14:20
 * <p/>
 * This adapter allows you to install (and uninstallcif needed) select-on-rollover behavior to any JList quickly without any additional
 * coding.
 */

public class ListRolloverSelectionAdapter implements MouseMotionListener
{
    private JList list;

    public ListRolloverSelectionAdapter ( JList list )
    {
        super ();
        this.list = list;
    }

    /**
     * Performs selection change on rollover
     */

    public void mouseMoved ( MouseEvent e )
    {
        // Disabled lists aren't affected
        if ( list.isEnabled () )
        {
            // Compute cell under point
            int index = list.locationToIndex ( e.getPoint () );
            if ( index != list.getSelectedIndex () )
            {
                // Change selection
                list.setSelectedIndex ( index );
            }
        }
    }

    /**
     * Ignored method
     */

    public void mouseDragged ( MouseEvent e )
    {
        //
    }

    /**
     * Installs rollover selection adapter and ensures that it is the only installed
     */

    public static ListRolloverSelectionAdapter install ( JList list )
    {
        // Uninstall old adapters first
        uninstall ( list );

        // Add new adapter
        ListRolloverSelectionAdapter adapter = new ListRolloverSelectionAdapter ( list );
        list.addMouseMotionListener ( adapter );
        return adapter;
    }

    /**
     * Uninstalls any existing rollover selection adapters from list
     */

    public static void uninstall ( JList list )
    {
        for ( MouseMotionListener listener : list.getMouseMotionListeners () )
        {
            if ( listener instanceof ListRolloverSelectionAdapter )
            {
                list.removeMouseMotionListener ( listener );
            }
        }
    }

    /**
     * Checks if list has any rollover selection adapter installed
     */

    public static boolean isInstalled ( JList list )
    {
        for ( MouseMotionListener listener : list.getMouseMotionListeners () )
        {
            if ( listener instanceof ListRolloverSelectionAdapter )
            {
                return true;
            }
        }
        return false;
    }
}